cmake_minimum_required(VERSION 2.6)

PROJECT (xrelayer)

SET (CMAKE_BUILD_TYPE Release)

# library setup

# Boost C++ Libraries
SET (Boost_LIB_SUFFIX ${BOOST_COMPILER}-mt-${BOOST_VERSION})
SET (Boost_FIND_COMPONENTS program_options filesystem serialization system thread regex)
INCLUDE (cmake/FindBoost.cmake)

# Wt web-based framework (optional)
IF (WEBGUI)
	INCLUDE (cmake/FindWt.cmake)
ENDIF(WEBGUI)

IF (LOG)
	FIND_PATH (LOG4CXX_INCLUDE NAMES log4cxx/log4cxx.h)
	IF (LOG4CXX_INCLUDE)
		MESSAGE (STATUS "Log4cxx include path is found - ${LOG4CXX_INCLUDE}")
	ENDIF (LOG4CXX_INCLUDE)

	FIND_LIBRARY (LOG4CXX_LIB NAMES log4cxx)
	IF (LOG4CXX_LIB)
		MESSAGE (STATUS "Log4cxx library path is found - ${LOG4CXX_LIB}")
	ENDIF (LOG4CXX_LIB)
ENDIF (LOG)

IF (WIN32)
	SET (WIN32_LIBS mswsock ws2_32 odbc32 iphlpapi winmm)
ELSE (WIN32)
	SET (LINUX_LIBS crypt pthread)
ENDIF (WIN32)

INCLUDE_DIRECTORIES (${Boost_INCLUDE_DIRS} lib/hippolib lib/rapidxml/include lib/md5)
IF (LOG)
	INCLUDE_DIRECTORIES (${LOG4CXX_INCLUDE})
ENDIF (LOG)
IF (WEBGUI)
	INCLUDE_DIRECTORIES (${Wt_INCLUDE_DIR})
ENDIF (WEBGUI)


ADD_DEFINITIONS (-DNDEBUG)
IF (LOG)
	ADD_DEFINITIONS(-DUSE_LOG4CXX -DLOG4CXX_STATIC)
ENDIF (LOG)
IF (WEBGUI)
	ADD_DEFINITIONS (-DGUI)
ENDIF (WEBGUI)

IF (UNIX)
	ADD_DEFINITIONS (-DUNIX)
ENDIF (UNIX)



# main program
SET (XRELAYER_SOURCES src/httpproxy/httpconfig.cpp src/httpproxy/httpmain.cpp
	src/httpproxy/httpproxy.cpp src/httpproxy/httpsession.cpp
	src/httpproxy/httpuserdb.cpp src/httpproxy/httpusermgr.cpp
	src/httpproxy/httpxmlcfg.cpp src/common/logmanager.cpp lib/md5/md5.c)
IF (WEBGUI)
	FILE (GLOB WEBGUI_SOURCES src/gui/*.cpp)
	SET (XRELAYER_SOURCES ${XRELAYER_SOURCES} ${WEBGUI_SOURCES})
ENDIF (WEBGUI)

IF (UNIX)
	SET (XRELAYER_SOURCES ${XRELAYER_SOURCES} src/common/signal.cpp)
ENDIF (UNIX)

ADD_EXECUTABLE (xrelayer ${XRELAYER_SOURCES})
IF (LOG)
	TARGET_LINK_LIBRARIES (xrelayer ${LOG4CXX_LIB})
ENDIF (LOG)
IF (WEBGUI)
	TARGET_LINK_LIBRARIES(xrelayer ${Wt_LIBRARY} ${Wt_HTTP_LIBRARY})
ENDIF (WEBGUI)
IF (WIN32)
TARGET_LINK_LIBRARIES (xrelayer ${WIN32_LIBS})
ELSE (WIN32)
TARGET_LINK_LIBRARIES (xrelayer ${LINUX_LIBS}
				${Boost_program_options_LIBRARIES}
				${Boost_filesystem_LIBRARIES}
				${Boost_serialization_LIBRARIES}
				${Boost_system_LIBRARIES}
				${Boost_thread_LIBRARIES}
				${Boost_regex_LIBRARIES})
ENDIF (WIN32)


# user accout database maintainer
SET (HTTPPASSWD_SOURCES src/httpproxy/httppasswd.cpp 
src/httpproxy/httpuserdb.cpp lib/md5/md5.c)
ADD_EXECUTABLE (xrpasswd ${HTTPPASSWD_SOURCES})
IF (WIN32)
TARGET_LINK_LIBRARIES (xrpasswd ${WIN32_LIBS})
ELSE (WIN32)
TARGET_LINK_LIBRARIES (xrpasswd ${LINUX_LIBS}
				${Boost_program_options_LIBRARIES}
				${Boost_filesystem_LIBRARIES}
				${Boost_serialization_LIBRARIES})
ENDIF (WIN32)


# xml config testing program
SET (CFGTEST_SOURCES src/httpproxy/httpconfig.cpp src/httpproxy/httpxmlcfg.cpp
    src/httpproxy/configtest.cpp)
ADD_EXECUTABLE (cfgtest ${CFGTEST_SOURCES})
SET_TARGET_PROPERTIES(cfgtest PROPERTIES COMPILE_FLAGS -DNO_INITLOG)
IF (WIN32)
TARGET_LINK_LIBRARIES (cfgtest ${WIN32_LIBS})
ELSE (WIN32)
TARGET_LINK_LIBRARIES (cfgtest ${LINUX_LIBS}
                                ${Boost_program_options_LIBRARIES}
                                ${Boost_filesystem_LIBRARIES}
                                ${Boost_serialization_LIBRARIES}
				${Boost_regex_LIBRARIES}
				${Boost_system_LIBRARIES})
ENDIF (WIN32)

# Installation

INSTALL (TARGETS xrelayer xrpasswd cfgtest DESTINATION xrelayer)
IF (UNIX)
INSTALL (CODE "CONFIGURE_FILE (${xrelayer_SOURCE_DIR}/src/conf/config.xml /etc/xrelayer.conf @ONLY)")
INSTALL (CODE "CONFIGURE_FILE (${xrelayer_SOURCE_DIR}/src/conf/xrelayer /etc/init.d/xrelayer @ONLY)")
INSTALL (CODE "EXECUTE_PROCESS (COMMAND chkconfig --add xrelayer)")
ENDIF (UNIX)

